<?php
class Product extends Base
{
	public $id;
	public $vg_sp_id;
	public $vg_dm_id;
	public $sp_name;
	public $sp_gia;
	public $vg_gh_ids;
	public $sp_lien_quan;
	public $tags;
	public $ngay_cn;

	public function initialize()
	{
		$this->setSource('san_pham');
		parent::initialize();
	}
	public function getFirstImage() 
	{
		$image = new Image();
		$first_image = $image::findFirst("vg_sp_id = $this->vg_sp_id");
		return $this->config->application->baseUri.'images/'.$first_image->vg_sp_id.'/'.$first_image->img_name;
	}
	public function getListImage() 
	{
		$image = new Image();
		$list_image = $image::find(array("vg_sp_id = $this->vg_sp_id","limit" => 3));
		$array_image = array();
		foreach ($list_image as $value) {
			$array_image[] = $this->config->application->baseUri.'images/'.$value->vg_sp_id.'/'.$value->img_name;
		}
		return $array_image;
	}
	public function getUrl() {
		//return '/priceasia/product/?id='.$this->id.'';
		return '/priceasia/san-pham/'.removeTitle($this->sp_name).'-'.$this->id.'.html';
	}
	public function getCountSell() {
		$spgh = Spgh::find(array(
				"vg_sp_id = $this->vg_sp_id"
			)
		);
		$count = count($spgh);
		return $count;
	}
	public function getListGh() {
		$array_spgh = Spgh::find(array(
				"vg_sp_id = $this->vg_sp_id",
				"limit" => 20
			)
		);
		$array_gh = array();
		$ival = 0;
		foreach ($array_spgh as $value) {
			$gh = Gianhang::findFirst("vg_gh_id = $value->vg_gh_id");
			$array_gh[$ival]['gh_avatar'] = $this->config->application->baseUri."avatars/".$gh->gh_avatar;
			$array_gh[$ival]['gh_uname'] = "$gh->gh_uname";
			$array_gh[$ival]['gia_sp'] = format_price($value->gia_sp);
			$array_gh[$ival]['gh_sp_uri'] = "$value->gh_sp_uri";
			$array_gh[$ival]['sp_tt_khac'] = "$value->sp_tt_khac";
			$ival++;
		}
		return $array_gh;
	}
	public function getProductRelate() {
		$array_product_relate = Product::find(array(
				"vg_sp_id IN($this->sp_lien_quan)",
				"limit" => 10
			)
		);
		return $array_product_relate;
	}
	public function getTags() {
		$arr_tags = array();
		if($this->tags != '') {
           $arr_tags = explode(',',$this->tags);
        }
		return $arr_tags;
	}
	public function getFormatPrice() {
		return format_price($this->sp_gia);
	}
}

?>